package com.ruoyi.productReturn.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pointRecord.dao.IProductReturnDao;
import com.ruoyi.pointRecord.domain.PointsExchangeRecord;
import com.ruoyi.pointRecord.mapper.PointsExchangeRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.productReturn.mapper.PointsReturnRequestMapper;
import com.ruoyi.productReturn.domain.PointsReturnRequest;
import com.ruoyi.productReturn.service.IPointsReturnRequestService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分商品退货管理Service业务层处理
 * 
 * @author amy
 * @date 2025-06-24
 */
@Service
public class PointsReturnRequestServiceImpl implements IPointsReturnRequestService 
{
    @Autowired
    private PointsReturnRequestMapper pointsReturnRequestMapper;

    @Autowired
    private PointsExchangeRecordMapper pointsExchangeRecordMapper;

    @Autowired
    private IProductReturnDao productReturnDao;

    /**
     * 查询积分商品退货管理
     * 
     * @param id 积分商品退货管理主键
     * @return 积分商品退货管理
     */
    @Override
    public PointsReturnRequest selectPointsReturnRequestById(Long id)
    {
        return pointsReturnRequestMapper.selectPointsReturnRequestById(id);
    }

    /**
     * 查询积分商品退货列表
     * 
     * @param pointsReturnRequest 积分商品退货管理
     * @return 积分商品退货管理
     */
    @Override
    public List<PointsReturnRequest> selectAllPointsReturnRequestList(PointsReturnRequest pointsReturnRequest)
    {
        return pointsReturnRequestMapper.selectAllPointsReturnRequestList(pointsReturnRequest);
    }

    /**
     * 查询用户商品退货列表
     *
     * @param pointsReturnRequest 积分商品退货管理
     * @return 积分商品退货管理
     */
    @Override
    public List<PointsReturnRequest> selectUserPointsReturnRequestList(PointsReturnRequest pointsReturnRequest) {
        pointsReturnRequest.setUserId(SecurityUtils.getUserId());
        return pointsReturnRequestMapper.selectUserPointsReturnRequestList(pointsReturnRequest);
    }

    /**
     * 新增积分商品退货管理
     * 
     * @param pointsReturnRequest 积分商品退货管理
     * @return 结果
     */
    @Override
    public int insertPointsReturnRequest(PointsReturnRequest pointsReturnRequest)
    {
        return pointsReturnRequestMapper.insertPointsReturnRequest(pointsReturnRequest);
    }

    /**
     * 修改积分商品退货管理
     * 
     * @param pointsReturnRequest 积分商品退货管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updatePointsReturnRequest(PointsReturnRequest pointsReturnRequest)
    {
        int result = pointsReturnRequestMapper.updatePointsReturnRequest(pointsReturnRequest);
        if (result <= 0) {
            return AjaxResult.error("审核失败");
        }

        // 如果审核通过，执行实际的退货操作
        if (pointsReturnRequest.getStatus() == 1) {
            PointsExchangeRecord exchangeRecord = pointsExchangeRecordMapper.selectPointsExchangeRecordById(pointsReturnRequest.getExchangeId());
            if (exchangeRecord != null) {
                // 执行积分退还和库存恢复
                productReturnDao.processExchangeReversal(exchangeRecord, 5L, 7L, "退货失败");
            }
        }
        return AjaxResult.success("审核完成");
    }

    /**
     * 批量删除积分商品退货管理
     * 
     * @param ids 需要删除的积分商品退货管理主键
     * @return 结果
     */
    @Override
    public int deletePointsReturnRequestByIds(Long[] ids)
    {
        return pointsReturnRequestMapper.deletePointsReturnRequestByIds(ids);
    }

    /**
     * 删除积分商品退货管理信息
     * 
     * @param id 积分商品退货管理主键
     * @return 结果
     */
    @Override
    public int deletePointsReturnRequestById(Long id)
    {
        return pointsReturnRequestMapper.deletePointsReturnRequestById(id);
    }
}
